using UnityEngine;
using System.Collections;

public class Switching : MonoBehaviour {

	public			Movement			AlphaMove;
	public			Movement			BetaMove;	
	
	private			Transform			RobAlpha;
	private			Transform			RobBeta;
	private			bool				m_SwitchPlayer = false;
	private			bool				m_MainPlayerControl = true;
	
	void Start ()
	{
		RobAlpha = GameObject.Find("RobAlpha").transform;
		RobBeta = GameObject.Find("RobBeta").transform;
		
		AlphaMove = RobAlpha.GetComponent("Movement") as Movement;
		BetaMove = RobBeta.GetComponent("Movement") as Movement;
	}
	
	void Update ()
	{
		KeyboardHandle();
		
		if (m_SwitchPlayer)
			SwitchPlayer();		
	}
	
	void KeyboardHandle()
	{
		if ((Input.GetKeyDown(KeyCode.Space)))
			m_SwitchPlayer = true;
	}
	
	void SwitchPlayer()
	{
		m_SwitchPlayer = false;
		m_MainPlayerControl = !m_MainPlayerControl;
		
		if (m_MainPlayerControl)
		{
			AlphaMove.m_Freeze = false;
			AlphaMove.rigidbody.constraints = 	RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
			
			BetaMove.m_Freeze = true;
			BetaMove.rigidbody.constraints = 	RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ	|
								    			RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
			
			AlphaMove.m_MyCamera.enabled = true;
			BetaMove.m_MyCamera.enabled = false;
		}
		else
		{
			BetaMove.m_Freeze = false;
			BetaMove.rigidbody.constraints = 	RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
			
			AlphaMove.m_Freeze = true;
			AlphaMove.rigidbody.constraints = 	RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ	|
								    			RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;	
			
			AlphaMove.m_MyCamera.enabled = false;
			BetaMove.m_MyCamera.enabled = true;
		}
	}	
}
